var startPos = 0, endPos = 0, isScrolling = 0;
document.addEventListener('touchstart', function (event) {
    var touch = event.targetTouches[0]; //touches数组对象获得屏幕上所有的touch，取第一个touch
    startPos = { x: touch.pageX, y: touch.pageY, time: +new Date }; //取第一个touch的坐标值
    isScrolling = 0; //这个参数判断是垂直滚动还是水平滚动
}, false);

document.addEventListener('touchmove', function (event) {
    //当屏幕有多个touch或者页面被缩放过，就不执行move操作
    if (event.targetTouches.length > 1 || event.scale && event.scale !== 1) return;
    var touch = event.targetTouches[0];
    endPos = { x: touch.pageX - startPos.x, y: touch.pageY - startPos.y };
    isScrolling = Math.abs(endPos.x) < Math.abs(endPos.y) ? 1 : 0; //isScrolling为1时，表示纵向滑动，0为横向滑动
    if (isScrolling === 0) {
        //alert(0);
        event.preventDefault(); //阻止触摸事件的默认行为，即阻止滚屏
    }
}, false);

document.addEventListener('touchend', function (event) {
    document.removeEventListener('touchmove', this, false);
    document.removeEventListener('touchend', this, false);
}, false);

// 返回一组事件对象,放在绑定事件前
var IDXgesinfo = [{
    startX: 0,
    // startY: 0,
    startL: 0,
    startT: 0,
    speed: 200
}, {
    page: 0,
    total: 5,
    speed: 200,
    s: 100,
    startT: 0,
    startX: 0
    // startY: 0
}, {
    startX: 0,
    // startY: 0,
    startT: 0,
    startL: 0,
    speed: 200,
    s: 100
}, {
    startX: 0,
    startY: 0,
    s: 100,
    page: 0,
    len: 6,
    startT: 0,
    // startB: 0,
    // startN: 0,
    // startA: 0,
    speed: 200
}];

function IDXtouchs(o, param) {
    var obj = o[param];
    switch (param) {
        case 0:
            var navDragBuffer = {
                touchstart: function (e) {
                    obj.startX = e.originalEvent.touches[0].pageX;
                    // obj.startY = e.originalEvent.touches[0].pageY;
                    obj.startL = !!parseInt($(this).css("marginLeft")) ? parseInt($(this).css("marginLeft")) : 0;
                    obj.startT = e.timeStamp;
                    $(this).stop();
                },
                touchmove: function (e) {
                    //e.preventDefault();
                    var mx = e.originalEvent.touches[0].pageX;
                    // var my = e.originalEvent.touches[0].pageY;
                    var bw = $(this).width();
                    var pw = $(this).parent().width();
                    if (bw <= pw) {
                        return;
                    }
                    var mL = obj.startL - obj.startX + mx;
                    // var dXY = Math.abs(my - obj.startY) - Math.abs(mx - obj.startX);
                    // if (dXY < 0) {
                    if (mL > 0) {
                        $(this).css("marginLeft", mL / 3);
                    } else if (mL < pw - bw) {
                        $(this).css("marginLeft", (pw - bw) * 2 / 3 + mL / 3);
                    } else {
                        $(this).css("marginLeft", mL);
                    }
                    // }
                },
                touchend: function (e) {
                    var tE = e.timeStamp;
                    var tD = tE - obj.startT;
                    var endML = parseInt($(this).css("marginLeft"));
                    var mL = $(this).parent().width() - $(this).width();
                    if (endML > 0) {
                        $(this).animate({ "marginLeft": "0" }, obj.speed);
                    }
                    if (endML < mL) {
                        $(this).animate({ "marginLeft": mL }, obj.speed)
                    }
                }
            }
            return navDragBuffer;
        case 1:
            var bannerDragBuffer = {
                touchstart: function (e) {
                    var k = obj.page;
                    var vw = -$(this).width();
                    // obj.page = !!parseInt($(this).css("marginLeft")) ? Math.round(parseInt($(this).css("marginLeft")) / vw) : 0;
                    obj.startX = e.originalEvent.touches[0].pageX;
                    obj.startT = e.timeStamp;
                    clearInterval(bannerTimer);
                    $(this).stop();
                },
                touchmove: function (e) {
                    // e.preventDefault();
                    var mx = e.originalEvent.touches[0].pageX;
                    var bw = $(this).width();
                    var pw = $(this).parent().width();
                    var mL = -obj.page * pw - obj.startX + mx;
                    if (mL < 0 && mL > pw - bw) {
                        $(this).css("marginLeft", mL);
                    }
                },
                touchend: function (e) {
                    var tE = e.timeStamp;
                    var tD = tE - obj.startT;
                    var ex = e.originalEvent.changedTouches[0].pageX;
                    var xD = ex - obj.startX;
                    var pw = $(this).parent().width();
                    if (xD >= obj.s && obj.page > 0) {
                        obj.page--;
                    }
                    if (xD <= -obj.s && obj.page < obj.total - 1) {
                        obj.page++;
                    }
                    if (tD < obj.speed && xD > 0 && xD < obj.s && obj.page > 0) {
                        obj.page--;
                    }
                    if (tD < obj.speed && xD < 0 && xD > -obj.s && obj.page < obj.total - 1) {
                        obj.page++;
                    }
                    var mL = -obj.page * pw;
                    $(".banner-point li").eq(obj.page).addClass("active").siblings().removeClass("active")
                    $(this).animate({ "marginLeft": mL }, 600);
                    bannerTimer = setInterval(bannerLoop, 4000);
                }
            }
            return bannerDragBuffer;
        case 2:
            var twoPageToggle = {
                touchstart: function (e) {
                    obj.startX = e.originalEvent.touches[0].pageX;
                    obj.startL = !!parseInt($(this).css("marginLeft")) ? parseInt($(this).css("marginLeft")) : 0;
                    obj.startT = e.timeStamp;
                    $(this).stop();
                },
                touchmove: function (e) {
                    //e.preventDefault();
                    var mx = e.originalEvent.touches[0].pageX;
                    var bw = $(this).width();
                    var pw = $(this).parent().width();
                    if (bw <= pw) {
                        return;
                    }
                    var mL = obj.startL - obj.startX + mx;
                    if (mL > 0) {
                        $(this).css("marginLeft", mL / 3);
                    } else if (mL < pw - bw) {
                        $(this).css("marginLeft", (pw - bw) * 2 / 3 + mL / 3);
                    } else {
                        $(this).css("marginLeft", mL);
                    }
                },
                touchend: function (e) {
                    var tE = e.timeStamp;
                    var tD = tE - obj.startT;
                    var ex = e.originalEvent.changedTouches[0].pageX;
                    var xD = ex - obj.startX;
                    var pw = $(this).parent().width();
                    var bw = $(this).width();
                    var mL = 0;
                    if ((xD > obj.s || (xD > 0 && tD < obj.speed)) && obj.startL == pw - bw + 30) {
                        mL = 0;
                    }
                    if ((xD <= obj.s && xD > 0) || (xD >= -obj.s && xD < 0)) {
                        mL = obj.startL;
                    }
                    if ((xD < -obj.s || (xD < 0 && tD < obj.speed)) && (obj.startL == 0 || obj.startL == pw - bw + 30)) {
                        mL = pw - bw + 30;
                    }
                    $(this).animate({ "marginLeft": mL }, 500)
                }
            }
            return twoPageToggle;
        case 3:
            var cardToggle = {
                touchstart: function (e) {
                    //event.preventDefault();            
                    obj.startX = e.originalEvent.touches[0].pageX;
                    obj.starty = e.originalEvent.touches[0].pagey;
                    // obj.startB = !!parseInt($(this).find(".activeB").css("marginLeft")) ? parseInt($(this).find(".activeB").css("marginLeft")) : 0;
                    // obj.startN = !!parseInt($(this).find(".active").css("marginLeft")) ? parseInt($(this).find(".active").css("marginLeft")) : 0;
                    // obj.startA = !!parseInt($(this).find(".activeA").css("marginLeft")) ? parseInt($(this).find(".activeA").css("marginLeft")) : 0;
                    obj.page = $(this).find(".active").index();
                    obj.len = $(this).find("li").length;
                    obj.startT = e.timeStamp;
                    // console.log(obj.startB + ":" + obj.startN + ":" + obj.startA);
                    // console.log(obj.p)
                    $(this).stop();
                },
                touchmove: function (e) {
                    //e.preventDefault();
                    // var mx = e.originalEvent.touches[0].pageX;
                    // // var bw = $(this).width();
                    // var mB = obj.startB - obj.startX + mx;
                    // var mN = obj.startN - obj.startX + mx;
                    // var mA = obj.startA - obj.startX + mx;
                    // $(this).find(".activeB").css("marginLeft", mB);
                    // $(this).find(".active").css("marginLeft", mN);
                    // $(this).find(".activeA").css("marginLeft", mA);                   
                },
                touchend: function (e) {
                    var tE = e.timeStamp;
                    var tD = tE - obj.startT;
                    var ex = e.originalEvent.changedTouches[0].pageX;
                    var ey = e.originalEvent.changedTouches[0].pageY;
                    var xD = ex - obj.startX;
                    var xE = ey - obj.starty;
                    if (xD > 0 && xD < obj.s && tE > obj.speed) {
                        // event.preventDefault();
                        $(this).find(".activeB").css("marginLeft", obj.startB);
                        $(this).find(".active").css("marginLeft", obj.startN);
                        $(this).find(".activeA").css("marginLeft", obj.startA);
                    }
                    if (xD < 0 && xD > -obj.s && tE > obj.speed) {
                        // event.preventDefault();
                        $(this).find(".activeB").css("marginLeft", obj.startB);
                        $(this).find(".active").css("marginLeft", obj.startN);
                        $(this).find(".activeA").css("marginLeft", obj.startA);
                    }
                    if (xD > obj.s || (obj.speed < 10 && xD > 0)) {
                        if (obj.page != 1) {
                            $(this).find(".activeA").removeClass("activeA").addClass("dnone");
                            $(this).find(".active").removeClass("active").addClass("activeA");
                            $(this).find(".activeB").removeClass("activeB").addClass("active").prev().removeClass("dnone").addClass("activeB");
                        }
                        if (obj.page == 1) {
                            $(this).find(".activeA").removeClass("activeA").addClass("dnone");
                            $(this).find(".active").removeClass("active").addClass("activeA");
                            $(this).find(".activeB").removeClass("activeB").addClass("active");
                            $(this).find("li").eq(obj.len - 1).removeClass("dnone").addClass("activeB");
                        }
                    }
                    if (xD < -obj.s || (obj.speed < 10 && xD < 0)) {
                        if (obj.page != obj.len - 2) {
                            $(this).find(".activeB").removeClass("activeB").addClass("dnone");
                            $(this).find(".active").removeClass("active").addClass("activeB");
                            $(this).find(".activeA").removeClass("activeA").addClass("active").next().removeClass("dnone").addClass("activeA");
                        }
                        if (obj.page == obj.len - 2) {
                            $(this).find(".activeB").removeClass("activeB").addClass("dnone");
                            $(this).find(".active").removeClass("active").addClass("activeB");
                            $(this).find(".activeA").removeClass("activeA").addClass("active");
                            $(this).find("li").eq(0).removeClass("dnone").addClass("activeA");
                        }
                    }
                }
            }
            return cardToggle;
    }
}
// banner计时滚动
var bannerTimer;

function bannerLoop() {
    var pw = $(".banner-wrap").width();
    var g = IDXgesinfo[1];
    if (g.page === g.total - 1) {
        $(".banner-img").css({ "marginLeft": 0, "display": "none" });
        $(".banner-img").fadeIn();
        g.page = -1;
    }
    if (g.page < g.total - 1) {
        g.page++;
    }
    $(".banner-point li").eq(g.page).addClass("active").siblings().removeClass("active");
    var mL = -g.page * pw;
    $(".banner-img").animate({ "marginLeft": mL }, 500);
};
$(document).ready(function () {
    /*
    // 有banner的页面，重置banner数;
    var imgLen = $(".banner-img img").length;
    if (!!imgLen) {
        IDXgesinfo[1].total = imgLen;
        $(".banner-img").css("width", 100 * imgLen + "%");
        $(".banner-img img").css("width", 100 / imgLen + "%");
    }
    // app下载关闭按钮
    var appStamp = window.localStorage.getItem("appStamp");
    if (!appStamp) {
        appStamp = 0;
    }
    var timeBoolen = false;
    var now = new Date();
    var nTime = now.getTime();
    if (nTime - appStamp >= 86400000) {
        timeBoolen = true;
        $(".app-download").show();   
    }
    $(".app-d-close .icon").on("touchstart", function () {
        var rnow = new Date();
        var nowStamp = rnow.getTime();
        window.localStorage.setItem("appStamp", nowStamp);
        $(".app-download").slideUp("fast", function () {
            $(".all-wrap").css("marginTop", $(".head-fix").height());
        });
        timeBoolen = false;
    });

    $(document).on("touchmove", function () {
        var scrollTop = $(document).scrollTop();
        if (scrollTop > 10 && timeBoolen) {
            $(".app-download").hide();
        }
        if (scrollTop < 10 && timeBoolen) {
            $(".app-download").show();
        }
        $(".all-wrap").css("marginTop", $(".head-fix").height());
    });
    if (!!$(".footer-custom .nav-car i").text()) {
        $(".footer-custom .nav-car i").show();
    }
    */
    // 页面头margin-top自适应
    $(".all-wrap").css("marginTop", $(".head-fix").height());

    //分类页增加点击样式
    $(".cat-nav ul li").click(function () {
        $(this).addClass('c-active2').siblings().removeClass();
    });
    // 导航条滑动
    $(".nav-drag ul").on(IDXtouchs(IDXgesinfo, 0));
    //隐藏app下载框
    $(".app-download").css({ display: "none" });
    //导航条定位
    var nli = $(".nav-drag ul").children().length;
    for (var i = 0; i < nli; i++) {
        if ($(".nav-drag ul").children().eq(i).hasClass("active")) {
            if (i > 4) {
                $(".nav-drag ul").css("margin-left", "-700px");
            }
        }
    }
    //详情页——促销信息-没有促销就不显示移除div
    if ($(".bb32-wrap .bb1-item").children().length == 0) {
        $(".bb32-wrap .bb1-item").remove();
    }
});
